package com.yanqu.road.server.manager.activity.biaoju;

import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuCityData;
import com.yanqu.road.pb.activity.BiaoJuProto;

import java.util.ArrayList;
import java.util.List;

public class CrossBiaoJuCityRankInfo {
    private int cityId;
    private List<CrossBiaoJuCityData> cityDataList = new ArrayList<>();

    public CrossBiaoJuCityRankInfo(int cityId) {
        this.cityId = cityId;
    }

    public int getCityId() {
        return cityId;
    }

    public synchronized void addCityData(CrossBiaoJuCityData cityData) {
        if (cityDataList.contains(cityData)) {
            return;
        }
        cityDataList.add(cityData);
    }

    public synchronized void sortCityRank() {
        for (CrossBiaoJuCityData cityData : cityDataList) {
            cityData.setRankScore(cityData.getScore());
            cityData.setRankTime(cityData.getRankTime());
        }

        cityDataList.sort((o1, o2) -> {
            int compare = Long.compare(o2.getScore(), o1.getScore());
            if (compare == 0) {
                return Long.compare(o1.getRankTime(), o2.getRankTime());
            }
            return compare;
        });

        for (int i = 0; i < cityDataList.size(); i++) {
            int rank = i + 1;
            CrossBiaoJuCityData cityData = cityDataList.get(i);
            if (cityData.getRankScore() == 0) {
                break;
            }
            cityData.setRank(rank);
        }
    }

    public synchronized List<CrossBiaoJuCityData> getCityDataList() {
        return cityDataList;
    }

    public synchronized String getLastFirstUnionId() {
        for (CrossBiaoJuCityData cityData : cityDataList) {
            if (cityData.getLastRank() == 1) {
                return cityData.getUnionId();
            }
        }
        return null;
    }
}
